package ui.controls;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import ui.components.ImageControl;
import ui.Assets;
import ui.control.switches.*;
import ui.control.binary.States;

public class Switch extends ImageControl implements ui.control.binary.Control {

	private States state;
	protected Image image;
	private SwitchBank owner;
	private int id;
	
	public Switch( Assets assets, SwitchBank owner, int id, SwitchColors color, SwitchSizes size ) {
		this.owner = owner;
		this.id = id;
		
		addMouseListener(
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					mouseDown( e );
					repaint();
				}
				public void mouseReleased( MouseEvent e ) {
					mouseUp( e );
					repaint();
				}
			}
		);
		
		if( color == SwitchColors.RED )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchRed16;
			else
				imagelist = assets.switchRed24;
		else if( color == SwitchColors.BLUE )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchBlue16;
			else
				imagelist = assets.switchBlue24;
		else if( color == SwitchColors.GREEN )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchGreen16;
			else
				imagelist = assets.switchGreen24;
		else if( color == SwitchColors.YELLOW )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchYellow16;
			else
				imagelist = assets.switchYellow24;
		else if( color == SwitchColors.AMBER )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchAmber16;
			else
				imagelist = assets.switchAmber24;
		else if( color == SwitchColors.BLACK )
			if( size == SwitchSizes._16 )
				imagelist = assets.switchBlack16;
			else
				imagelist = assets.switchBlack24;
		
		if( size == SwitchSizes._16 ) {
			width = 16;
			height = 16;
		} else {
			width = 24;
			height = 24;
		}
		setSize( width, height);
		state = States.OFF;
		currentImage = 0;
		image = imagelist.image( currentImage );
	}
	public void setState( States state ) {
		switch( state ) {
		case OFF:
			currentImage = 0;
			image = imagelist.image( currentImage );
			this.state = States.OFF;
			repaint();
			break;
		case ON:
			currentImage = 1;
			image = imagelist.image( currentImage );
			this.state = States.ON;
			repaint();
			break;
		}
	}
	public States getState( ) {
		return state;
	}
	public States toggle( ) {
		switch( state ) {
		case OFF:
			setState( States.ON );
			break;
		case ON:
			setState( States.OFF );
			break;
		}
		return state;
	}
	public void toggleOn( ) {
		setState( States.ON );
	}
	public void toggleOff( ) {
		setState( States.OFF );
	}
	@Override public void mouseDown( MouseEvent e ) {
		toggle( );
		owner.changed( id, state );
	}
	@Override public void mouseUp( MouseEvent e ) {
		//toggle( );
	}
}
